<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------
/**
 * Class Evaluation
 * Desc  评价控制器
 * Create on 2024/10/25 17:53
 * Create by wangyafang
 */

namespace app\api\controller\wddj\user\evaluation;


use app\api\model\wddj\driver\Driver;
use app\api\model\wddj\User;
use app\api\model\wddj\user\Order;
use app\common\controller\Api;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

class Evaluation extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['submit','details'];
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\api\model\wddj\user\evaluation\Evaluation();
    }

    /**
     * Desc 评价
     * Create on 2024/10/22 10:50
     * Create by wangyafang
     */
    public function submit()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $wechat_id = User::getWechatId($this->auth->id);

        $param = $this->request->post();
        $result = $this->validate($param,'app\api\validate\wddj\user\evaluation\Evaluation');
        if(true !== $result){
            // 验证失败 输出错误信息
            $this->error($result);
        }

        $order_id = $param['order_id'];
        $orderObj = (new Order())->get($order_id);

        if ($orderObj['state'] != 'completed') {
            $this->error('订单未完成，无法评价');
        }
        if ($orderObj['wechat_id'] != $wechat_id) {
            $this->error('不是本人，无法操作');
        }
        $evaluationObj = $this->model->where('wechat_id',$wechat_id)->where('order_id',$order_id)->find();
        if ($evaluationObj) {
            $this->error('已评价');
        }

        $create_data = array(
            'wechat_id'=>$wechat_id,
        );
        $create_data = array_merge($create_data,$param);

        Db::startTrans();
        try {
            $this->model->data($create_data);
            $this->model->allowField(true)->save();
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success('评价成功');
    }

    /**
     * Desc 评价详情
     * Create on 2024/10/22 10:51
     * Create by wangyafang
     */
    public function details()
    {
        if(!$this->request->isGet()) {
            $this->error('请求类型错误');
        }
        $wechat_id = User::getWechatId($this->auth->id);

        $order_id = $this->request->get('order_id');
        $evaluationObj = $this->model->where('order_id',$order_id)->field('satisfaction_status,driver_id,content,remarks,wechat_id')->find();
        if (!$evaluationObj) {
            $this->error('评价信息不存在');
        }
        if ($evaluationObj['wechat_id'] != $wechat_id) {
            $this->error('不是本人，无法查看');
        }
        $orderObj = (new Order())->get($order_id);
        $driverObj = Driver::getDriverBaseInfo($orderObj['driver_id']);
        $evaluationObj['driver_info'] = $driverObj;
        $evaluationObj->hidden(['driver_id','wechat_id']);
        $this->success('请求成功',$evaluationObj);
    }
}



 